package cn.iocoder.yudao.module.erp.controller.admin.business.money.moneymethod;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneymethod.vo.MoneyMethodPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneymethod.vo.MoneyMethodRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneymethod.vo.MoneyMethodSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneymethod.MoneyMethodDO;
import cn.iocoder.yudao.module.erp.service.business.money.moneymethod.MoneyMethodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收/付款方式")
@RestController
@RequestMapping("/erp/money-method")
@Validated
public class MoneyMethodController {

    @Resource
    private MoneyMethodService moneyMethodService;

    @PostMapping("/create")
    @Operation(summary = "创建收/付款方式")
    @PreAuthorize("@ss.hasPermission('erp:money-method:create')")
    public CommonResult<Long> createMoneyMethod(@Valid @RequestBody MoneyMethodSaveReqVO createReqVO) {
        return success(moneyMethodService.createMoneyMethod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收/付款方式")
    @PreAuthorize("@ss.hasPermission('erp:money-method:update')")
    public CommonResult<Boolean> updateMoneyMethod(@Valid @RequestBody MoneyMethodSaveReqVO updateReqVO) {
        moneyMethodService.updateMoneyMethod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收/付款方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:money-method:delete')")
    public CommonResult<Boolean> deleteMoneyMethod(@RequestParam("id") Long id) {
        moneyMethodService.deleteMoneyMethod(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收/付款方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:money-method:query')")
    public CommonResult<MoneyMethodRespVO> getMoneyMethod(@RequestParam("id") Long id) {
        MoneyMethodDO moneyMethod = moneyMethodService.getMoneyMethod(id);
        return success(BeanUtils.toBean(moneyMethod, MoneyMethodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收/付款方式分页")
    @PreAuthorize("@ss.hasPermission('erp:money-method:query')")
    public CommonResult<PageResult<MoneyMethodRespVO>> getMoneyMethodPage(@Valid MoneyMethodPageReqVO pageReqVO) {
        PageResult<MoneyMethodDO> pageResult = moneyMethodService.getMoneyMethodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoneyMethodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收/付款方式 Excel")
    @PreAuthorize("@ss.hasPermission('erp:money-method:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoneyMethodExcel(@Valid MoneyMethodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoneyMethodDO> list = moneyMethodService.getMoneyMethodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收/付款方式.xls", "数据", MoneyMethodRespVO.class,
                        BeanUtils.toBean(list, MoneyMethodRespVO.class));
    }

}